package com.fubowen.reader.ui.download

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.URLUtil
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.fubowen.reader.R
import com.fubowen.reader.adapter.AutoGridLayoutManager
import com.fubowen.reader.adapter.SimpleListAdapter
import com.fubowen.reader.databinding.FragmentScanWebStartPageBinding
import com.fubowen.reader.databinding.HistoriesBinding
import com.fubowen.reader.databinding.SuggestBinding
import com.fubowen.reader.db.model.Suggest
import com.fubowen.reader.db.bean.WebSiteBean
import com.fubowen.reader.extension.dpToPx
import com.fubowen.reader.extension.hideSoftInput
import com.fubowen.reader.extension.replace
import com.fubowen.reader.extension.replaceAll
import com.fubowen.reader.extension.setEmpty
import com.fubowen.reader.extension.setVisible
import com.fubowen.reader.ui.fragment.ThemeFragment
import kotlinx.coroutines.launch


class ScanWebInitFragment : ThemeFragment() {

    private lateinit var binding: FragmentScanWebStartPageBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = FragmentScanWebStartPageBinding.inflate(inflater)
        binding.scan.setOnClickListener { scan() }
        binding.clear.setOnClickListener { binding.url.setEmpty() }
        binding.url.apply {
            addTextChangedListener { binding.clear.setVisible(!it.isNullOrBlank()) }
            setOnFocusChangeListener { _, hasFocus ->
                if (hasFocus)
                    binding.init.transitionToEnd()
                else
                    binding.init.transitionToStart()
            }
        }
        initSuggest()
        initHistories()
        return binding.root
    }

    //region 初始化建议列表
    private fun initSuggest() {
        val grid = dpToPx(24)
        val gap = dpToPx(8)
        val adapter = SimpleListAdapter<Suggest>(R.layout.suggest, requireContext()) {
            val vb = SuggestBinding.bind(it)
            it.setOnClickListener {
                binding.url.replace(vb.name.text)
            }
        }
        binding.suggest.layoutManager = AutoGridLayoutManager(grid, gap, gap)
        binding.suggest.adapter = adapter
        adapter.submitList(Suggest.defaultValue())
    }
    //endregion

    //region 初始化历史记录
    private fun initHistories() {
        val adapter = SimpleListAdapter<WebSiteBean>(R.layout.histories, requireContext()) {
            val vb = HistoriesBinding.bind(it)
            it.setOnClickListener { binding.url.replaceAll(vb.url.text) }
        }
        binding.histories.layoutManager = LinearLayoutManager(requireContext())
        binding.histories.adapter = adapter
        lifecycleScope.launch {
            val siteList = database.webSiteDao().all()
            adapter.submitList(siteList)
        }
    }
    //endregion

    private fun scan() {
        val url = binding.url.text.toString()
        if (URLUtil.isValidUrl(url)) {
            val parent = parentFragment
            if (parent is IScan) {
                binding.url.clearFocus()
                binding.url.hideSoftInput()
                parent.scan(url)
            }
        }
    }
}

interface IScan {
    fun scan(url: String)
}